<?php
require('config.php');
require('emotion.json.php');
	$wb_id=$_REQUEST['wb_id'];
	$user_id=$_REQUEST['user_id'];
	$content=$_REQUEST['content'];
	$img_list=$_REQUEST['img_list'];
	$money=$_REQUEST['money'];
	if($isEmotion==1)
        {
        	for($i=0;$i<count($emotion_json);$i++)
	        {
	        	$content=str_replace($emotion_json[$i]['text'],"<img src=\"../emotion/".$emotion_json[$i]['name'].".png\">",$content);
	        }
        }
	$sql="select * from ".$oto."_help where id='{$wb_id}'";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$help[]=$row;//将取得的所有数据赋值给person_info数组
	}
	$fabu_id=$help[0]['id'];
	$fabu_user_id=$help[0]['userId'];
	$time=date('Y-m-d H:i:s');
	$sql="select * from ".$oto."_help_order_user where help_id='{$wb_id}' and user_id='{$user_id}' and isDel='0'";
	$result=$db->query($sql);
	while($row=$result->fetch_assoc()){
		$isJiedan[]=$row;//将取得的所有数据赋值给person_info数组
	}
	if(isset($isJiedan))
	{
		$data[0]['pan']="-1";
	    $data[0]['msg']="你已经接过单了";
	}
	else
	{
		$sql="INSERT INTO ".$oto."_help_order_user (`help_id`,`user_id`,`money`,`createTime`) VALUES ('$wb_id','$user_id','$money','$time')";
	    $result2=$db->query($sql);
	    $csh='报价：￥'.$money;
	    $sql="INSERT INTO ".$oto."_help_chat (`helpId`,`userId`,`acceptUserId`,`content`,`createTime`,`type`) VALUES ('$wb_id','$user_id','$fabu_user_id','$csh','$time','1')";
	    $result=$db->query($sql);
	    $sql="INSERT INTO ".$oto."_help_chat (`helpId`,`userId`,`acceptUserId`,`content`,`createTime`) VALUES ('$wb_id','$user_id','$fabu_user_id','$content','$time')";
	    $result=$db->query($sql);
	    if($img_list!='')
	    {
	    	$img_list=explode("|",$img_list);
		    for($i=0;$i<count($img_list);$i++)
		    {
		    	$lin_tu='<img class="tutu" src="'.$img_list[$i].'" />';
		    	$sql="INSERT INTO ".$oto."_help_chat (`helpId`,`userId`,`acceptUserId`,`content`,`createTime`) VALUES ('$wb_id','$user_id','$fabu_user_id','$lin_tu','$time')";
		   		$result=$db->query($sql);
		    }
	    }
	    
	    //**增加接单者消息提醒***///
	    $sql="select * from ".$oto."_help_message where helpId='{$wb_id}' and userId='{$fabu_user_id}' and msg_type='0' and isDel='0'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$message[]=$row;//将取得的所有数据赋值给person_info数组
		}
		//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
		//file_put_contents("tsxx.txt", "\r\n".isset($message)."\r\n", FILE_APPEND);
		if(isset($message))
		{
			$lin_msg_id=$message[0]['id'];
			$sql="UPDATE ".$oto."_help_message SET `createTime`='{$time}' where id='{$lin_msg_id}'";
			$result=$db->query($sql);
		}
		else
		{
			$sql="INSERT INTO ".$oto."_help_message (`helpId`,`userId`,`msg_type`,`createTime`) VALUES ('$wb_id','$fabu_user_id','0','$time')";
	    	$result=$db->query($sql);
		}

		$sql="select * from ".$oto."_help_message where helpId='{$wb_id}' and userId='{$user_id}' and msg_type='2' and isDel='0'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$message2[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($message2))
		{
			$lin_msg_id=$message2[0]['id'];
			$sql="UPDATE ".$oto."_help_message SET `createTime`='{$time}' where id='{$lin_msg_id}'";
			$result=$db->query($sql);
		}
		else
		{
			$sql="INSERT INTO ".$oto."_help_message (`helpId`,`userId`,`msg_type`,`createTime`) VALUES ('$wb_id','$user_id','2','$time')";
	    	$result=$db->query($sql);
		}

		//***//
	    if($result2)
	    {
	    	$data[0]['pan']="true";
	    }
	    else
	    {
	    	$data[0]['pan']="-1";
	    	$data[0]['msg']="接单失败";
	    }
	}
	echo json_encode($data);
?>